package com.test;

import java.io.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;



public class LogUtil {
	private String logFilePath = "";
	private String formatFilePath = "";
	
	public LogUtil(String logFilePath,String formatFilePath){
		this.logFilePath = logFilePath;
		this.formatFilePath = formatFilePath;
	}
	
	public void writeFormatFile(String str){
		
	}
	
	public void formatLog(){		
		try {
			List<FormatPojo> resultList = new ArrayList<FormatPojo>();
			BufferedReader br=new BufferedReader(new InputStreamReader(new FileInputStream(new File(this.logFilePath))));
			String str=""; 
			String[] tt = null;
			int faileNum = 0;
			while((str=br.readLine())!=null){ 
				if(str.indexOf("testName:")>0){
					str = str.substring(28,str.length());
					FormatPojo fpj = new FormatPojo();
					tt = str.split(":");
					if(tt.length==3){
						fpj.setTitle(tt[0]);
						fpj.setMethod(tt[1]);
						fpj.setUseTime(tt[2]);
						resultList.add(fpj); 
					}	
				}else if(str.indexOf("can't get response")>0){
					faileNum++;
				}
			}				
			System.out.println("faile num="+faileNum);
			String method = "";
			long averageCount = 0;
			long count = 0;
			int s = 0;
			List<FormatPojo> tempList = null;
			for(int j=0;j<Constants.requestNames.length;j++){	
				System.out.println("\r>>>>>>>>>>>>>"+Constants.requestNames[j]+"<<<<<<<<<<<<");
				tempList = new ArrayList<FormatPojo>();
				for(int i=0;i<resultList.size();i++){
					if(resultList.get(i).getMethod().indexOf(Constants.requestNames[j])!=-1){
						tempList.add(resultList.get(i));
					}			
				}
				
				Collections.sort(tempList);
				for(int i=0;i<tempList.size();i++){
					count += Long.parseLong(tempList.get(i).getUseTime());
					System.out.println(tempList.get(i).getTitle().replace("testName","")+":"+tempList.get(i).getUseTime());
					s ++;
				}
				if(s>0){
					averageCount = count/s;					
					System.out.println("rquest number:"+s);
					System.out.println("average using time:"+averageCount);
					s = 0;
					count = 0;
					averageCount = 0;
				}
			}
		} catch (IOException e) {			
			e.printStackTrace();
		} 
	}
	
	public static void main(String[] argvs){
		LogUtil logUtil = new LogUtil("E:\\Workspaces\\bluestone\\info.log", "E:\\Workspaces\\bluestone\\infoResult.log");
		logUtil.formatLog();
	}
}
